<template>
  <el-dialog
    title="新增员工"
    width="600px"
    :visible="isShow"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment">
          <el-option
            v-for="item in employessData.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- 自定义指令点击其他空白区域关闭树形组件弹框 -->
        <div v-outClick="closeTree">
          <el-input
            v-model="form.departmentName"
            readonly
            @focus="foucsEvent"
          />
          <el-tree
            v-if="treeShow"
            :data="treeData"
            :props="{ label: 'name' }"
            :default-expand-all="true"
            @node-click="nodeClick"
          />
        </div>
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button type="primary" @click="sumbit">提交</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import employessData from '@/api/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
import { sysUserPost } from '@/api/employees.js'
import { validatePhone } from '@/utils/validate.js'
export default {
  data() {
    return {
      treeShow: false,
      treeData: [],
      isShow: false,
      employessData,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            validator: (rule, value, callback) => {
              validatePhone(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            }
          }
        ],
        formOfEmployment: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        workNumber: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        departmentName: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        correctionTime: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    async foucsEvent() {
      this.treeShow = true
      if (this.treeData.length === 0) {
        const res = await companyDepartment()
        this.treeData = this.changeData(res.data.depts, '')
      }
    },
    nodeClick(data) {
      this.treeShow = false
      this.form.departmentName = data.name
    },
    changeData(arr, pid) {
      return arr.filter((item) => {
        if (item.pid === pid) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    closeEvent() {
      this.isShow = false
      // 自动清除数据校验以及手动清除数据内容
      this.$refs.form.resetFields()
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    },
    closeTree() {
      this.treeShow = false
    },
    sumbit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPost(this.form)
          this.isShow = false
          this.$message.success('添加成功')
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style></style>
